<template>
  <div id="supplierManage">
    <h2>供应商基本资料</h2>
    <el-divider></el-divider>
    <el-button type="primary" style="margin-bottom: 20px" @click="addSupplier">增加供应商</el-button>
    <el-table :data="supplierList" border style="width: 100%">
      <el-table-column label="序号" width="80" align="center">
        <template slot-scope="scope">{{scope.$index+1}}</template>
      </el-table-column>
      <el-table-column prop="name" label="供应商名称" width="180" align="center"></el-table-column>
      <el-table-column prop="supplierCode" label="供应商编码" width="180" align="center"></el-table-column>
      <el-table-column prop="phone" label="联系电话" width="180" align="center"></el-table-column>
      <el-table-column prop="contacts" label="联系人" width="180" align="center"></el-table-column>
      <el-table-column prop="contactsAddress" label="地址" align="center"></el-table-column>
      <el-table-column prop="operation" label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="primary" @click="editInfo(scope.row)">编辑</el-button>
          <el-button type="danger" style="margin-left: 16px" @click="delInfo(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <SupplierDailog v-if="isShowDailog" :type="type" :dataInfo="dataInfo" @cancel="cancel" />
  </div>
</template>

<script>
import SupplierDailog from "./SupplierDailog.vue";
import { getSupplierList, deleteSupplier } from "@/http/supplierManagementApi";
export default {
  created() {
    this.initList();
  },
  components: {
    SupplierDailog
  },
  data() {
    return {
      type: "",
      isShowDailog: false,
      dataInfo: null,
      supplierList: [
        {
          id: 1,
          supplierCode: "ZSGS",
          name: "浙商公司",
          contacts: "张三",
          phone: "11111",
          contactsAddress: "gxa",
          fax: "1",
          bankInfor: "1",
          mailAddress: "1",
          remarks: "1",
          is_delete: 1
        }
      ]
    };
  },
  methods: {
    // 添加供应商
    addSupplier() {
      this.type = "add";
      this.isShowDailog = true;
      this.dataInfo = null;
    },
    cancel() {
      this.isShowDailog = false;
      this.dataInfo = null;
      this.initList();
    },
    // 修改数据
    editInfo(data) {
      this.type = "edit";
      this.isShowDailog = true;
      this.dataInfo = data;
    },
    // 删除数据
    delInfo(id) {
      this.$confirm("是否继续此操作?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteSupplier(id)
            .then(() => {
              this.initList();
            })
            .catch(err => {
              console.log(err);
            });
        })
        .catch(() => {});
    },
    // 初始化列表
    initList() {
      getSupplierList()
        .then(res => {
          this.supplierList = res.data.data;
        })
        .catch(err => {
          console.log(err);
        });
    }
  }
};
</script>

<style>
</style>